home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15022 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  82 lines

  1. Path: mojo.eng.umd.edu!usenet
  2. From: Vinay Chande <chande@eng.umd.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: help! machine dependent behavior of libg++ iofstream functions
  5. Date: Tue, 02 Apr 1996 23:56:47 -0500
  6. Organization: Department of Electrical Engineering, University of Maryland, College Park.
  7. Message-ID: <3162050F.41C6@eng.umd.edu>
  8. NNTP-Posting-Host: baud.eng.umd.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; OSF1 V3.2 alpha)
  13.  
  14. Subject:
  15. help! machine dependent behavior of libg++ fstream functions.
  16.  
  17. Please help.
  18.  
  19. I have two files Write.cxx and Read.cxx  containing the 
  20. following. 
  21.  
  22.  --------------
  23. //Write.cxx
  24.  
  25. #include<fstream.h>
  26. main()
  27.     int a = 0x12abcd34;
  28.     ofstream  outstream("data/junk");
  29.     outstream.write(&a, sizeof(int));
  30.     outstream.close();
  31.  
  32. }
  33.  
  34. ------------------------------
  35.  
  36. // Read.cxx
  37. #include<fstream.h>
  38. #include<stream.h>
  39. main()
  40. {
  41.     int a;
  42.     ifstream instream("data/junk");
  43.     instream.read(&a, sizeof(int));
  44.     instream.close();    
  45.  
  46.     cout << "a is " << hex(a) <<endl;
  47.     
  48.  
  49. }
  50.  
  51. -- -----------
  52.  
  53. I compile both of them using g++.
  54.  
  55. I run Write.cxx on a sun machine and create the file "data/junk".
  56. I run Read.cxx on a DEC alpha. 
  57.  
  58. You guessed it. Read operation yields 
  59.  
  60. a is 34cdab12
  61.  
  62. (The byte order is  reversed.)
  63.  
  64. Same thing happens if I reverse machines for reading and writing.
  65. (If both programs are run on same machine, the result is always
  66. correct.)
  67.  
  68. What do I do to make both the machines the correct value of a? 
  69. ( My actual problem involves reading floating point numbers from a 
  70. binary file. How do I read the correct floating point numbers
  71. irrespective of the machine which generated the file? ) 
  72.  
  73. Please send me email.
  74. Thank you very much
  75. -Vinay 
  76.  
  77. ------Vinay Chande----chande@eng.umd.edu---------------------
  78.                  _| |_    
  79. -------------"?"------------o('v`)o--------------"!"-------------
  80.  
  81.